var idNumber,name;

const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' +
  'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=='

var isLock = false;

$(function() {
	$("#loginFun").click(function(){
		loginFun();
	})
    $("#goNext").click(function(){
        $("#login").show()
        $("#draw").hide()
        $("#show").hide()
        $("#homepage").hide()
    })
	$("#drawFun").click(function(){
	    if(!isLock){
	        isLock = true;
		    draw();
	    }else{
	        $.alert("请勿重复点击");
	    }
	})
})
//登录
function loginFun(){
	name = $("#nameIn").val().trim();
	idNumber = $("#idNumberIn").val().trim();
	$("#idNumberIn").val(idNumber);
	if(idNumber==""||name==""){
		$.alert("请输入相关信息");
		return;
	}
	name = encryptFun(name);
	idNumber = encryptFun(idNumber);
	console.log(name)
	console.log(serviceIp)
    $.ajax({
        type:'POST',
        url:serviceIp+'/api/getByIdNumber',
        dataType:"json",
        data:{"idNumber":idNumber,"userName":name},
        success:function(res){
            console.log(res);
			if(res.code!=0){
				$.alert(res.msg);
			}
			if(res.msg=='1'){
				$("#login").hide()
				$("#draw").show()
				$("#show").hide()
				$("#house").hide()
                $("#homepage").hide()
			}else if(res.msg=='2'){
				var groupDetail = res.groupDetail;
				var groupInfo = res.groupInfo;
                showInfo(groupDetail,groupInfo);
			}
        },
        error:function(jqXHR, textStatus, errorThrown){
            console.log(errorThrown);
        }
    })
}


function draw(){
    $.ajax({
        type:'POST',
        url:serviceIp+'/api/group',
        dataType:"json",
        data:{"idNumber":idNumber},
        success:function(res){
            console.log(res);
            isLock = false;
			if(res.code!=0){
				$.alert(res.msg);
				$("#login").show()
				$("#draw").hide()
				$("#show").hide()
				$("#house").hide()
                $("#homepage").hide()
				return ;
			}
			var groupDetail = res.groupDetail;
			var groupInfo = res.groupInfo;
			showInfo(groupDetail,groupInfo);
        },
        error:function(jqXHR, textStatus, errorThrown){
            console.log(errorThrown);
        }
    })
}

//显示信息
function showInfo(groupDetail,groupInfo){
    $("#login").hide()
    $("#draw").hide()
    $("#show").show()
    $("#homepage").hide()
    $("#name").html(groupDetail.userName);
    $("#idNumber").html(groupDetail.idNumber);
    $("#time").html(groupInfo.beginTime);
    $("#endTime").html(groupInfo.endTime);
    $("#num").html(groupDetail.userNo);
    $("#group").html("第"+groupInfo.groupName+"组");
    if(groupDetail.status===1){
        $("#house").show()
        $("#address").html(groupDetail.address);
        $("#roomNo").html(groupDetail.roomNo);
        $("#huxing").html(groupDetail.huxing);
        $("#areaSpace").html(groupDetail.areaSpace);
        $("#roomSpace").html(groupDetail.roomSpace);
    }
}

//参数加密
function encryptFun(param){
    const encryptor = new JSEncrypt()
    encryptor.setPublicKey(publicKey) // 设置公钥
    var encrypted  = encryptor.encrypt(param) // 对数据进行加密
    // 加密后的密文
    console.log(encrypted);
    return encrypted;
}

// 解密
/*
function decrypt(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey) // 设置私钥
  return encryptor.decrypt(txt) // 对数据进行解密
}*/
